package com.smzd.framework.system.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.MenuService;
import com.smzd.framework.system.domain.Function;
import com.smzd.framework.system.domain.Menu;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.utils.db.RPage;

@Controller
@RequestMapping("/system/menus")
@Access(Role.TAG_ROLE_MENUS)
public class MenuController extends AbstractController<Menu> {

    @Autowired
    private MenuService service;

    @Override
    protected IFGService<Menu> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        Menu scope = elem.getScope();
        if (scope != null) {
            if (scope.getId() == null || scope.getId() == 0) {
                elem.setScope(null);
            }
        }
        Function ref = elem.getReference();
        if (ref != null) {
            if (ref.getId() == null || ref.getId() == 0) {
                elem.setReference(null);
            }
        }
        return super.postAddData(model, elem, result);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        return super.toListPage(model, elem, result);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model, RPage<Menu> page, BindingResult result) throws Exception {
        return super.getListData(model, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        Function ref = elem.getReference();
        if (ref != null) {
            if (ref.getId() == null || ref.getId() == 0) {
                elem.setReference(null);
            }
        }
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }

    /**
     * 获得一个菜单项用于显示和修改
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String getMenuInfo(@PathVariable("id") int id) throws Exception {
        String json = service.getMenuInfoById(id);
        return json;
    }

    /**
     * 调整菜单项的位置。UI中拖拉菜单调整其顺序，或者是其所属的父级菜单。
     * 
     * @param model
     * @param elem
     * @param result
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/position", method = RequestMethod.POST)
    @ResponseBody
    public String updateMenuPosition(Map<String, Object> model, Menu elem, BindingResult result) throws Exception {
        Menu scope = elem.getScope();
        if (scope != null) {
            if (scope.getId() == null || scope.getId() == 0) {
                elem.setScope(null);
            }
        }
        String json = service.updateMenuPosition(elem);
        return json;
    }
}
